package simulator.hardware;

import java.util.List;
import java.util.Vector;

import simulator.software.ProcessorRunnable;

/**
 * Represents processor hardware in a simulation. Hosts a single
 * <code>ProcessorRunnable</code> at a time. Delegates time (steps) to the
 * resident <code>ProcessorRunnable</code>.
 * 
 * @author Dave LaPointe
 * @author Kyle Loomis
 * 
 */
public class Processor
{
   protected String id;
   protected ProcessorRunnable current;
   protected List<ProcessorRunnable> history = null;

   /**
    * Creates a new <code>Processor</code> with the specified unique id.
    * 
    * @param id
    *           unique identifier for the processor
    */
   public Processor(String id)
   {
      this.setId(id);
      history = new Vector<ProcessorRunnable>();
   }

   /**
    * Gets the unique id of the processor.
    * 
    * @return the unique id of the processor.
    */
   public String getId()
   {
      return this.id;
   }

   /**
    * Sets the unique id of the processor
    * 
    * @param id
    *           the unique id of the processor
    */
   public void setId(String id)
   {
      this.id = id;
   }

   /**
    * Issues a single step, or time unit, to its resident
    * <code>ProcessorRunnable</code>.
    */
   public void step()
   {
      history.add(current);

      if (current != null)
      {
         current.step();
      }
   }

   /**
    * Loads the specified <code>ProcessorRunnable</code>. Any previous resident
    * is evicted to make room.
    * 
    * @param p
    *           the <code>ProcessorRunnable</code> to load
    */
   public void load(ProcessorRunnable p)
   {
      current = p;
   }

   /**
    * Gets the currently running <code>ProcessorRunnable</code> on this
    * processor.
    * 
    * @return the currently running resident
    */
   public ProcessorRunnable getRunningProcess()
   {
      return current;
   }

   /**
    * Gets a <code>List</code> of <code>ProcessorRunnable</code> in the order in
    * which they were placed on the processor to run
    * 
    * @return a <code>List</code> of <code>ProcessorRunnable</code> objects in
    *         historical run-order
    */
   public List<ProcessorRunnable> getHistory()
   {
      return history;
   }
}